package com.mall.web.controller.product;

import com.mall.common.annotation.Log;
import com.mall.common.core.controller.BaseController;
import com.mall.common.core.domain.AjaxResult;
import com.mall.common.core.page.TableDataInfo;
import com.mall.common.enums.BusinessType;
import com.mall.common.utils.StringUtils;
import com.mall.common.utils.poi.ExcelUtil;
import com.mall.product.domain.biz.ProCustomRecordBiz;
import com.mall.product.domain.entity.ProCustomRecord;
import com.mall.product.service.ProCustomRecordService;
import com.mall.product.service.ProTypeService;
import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

/**
 * 商品自定义档案Controller
 *
 * @author zhonglin
 * @date 2020-05-07
 */
@Controller
@RequestMapping("/product/customRecord")
public class ProCustomRecordController
    extends BaseController {

  private String prefix = "product/customRecord";

  @Autowired
  private ProCustomRecordService proCustomRecordService;
  @Autowired
  private ProTypeService         proTypeService;

  @RequiresPermissions("product:customRecord:view")
  @GetMapping()
  public String customRecord() {

    return prefix + "/customRecord";
  }

  /**
   * 查询商品自定义档案列表
   */
  @RequiresPermissions("product:customRecord:list")
  @PostMapping("/list")
  @ResponseBody
  public TableDataInfo list(ProCustomRecord proCustomRecord) {

    startPage();
    List<ProCustomRecordBiz> list = proCustomRecordService.selectProCustomRecordList(proCustomRecord);
    return getDataTable(list);
  }

  /**
   * 导出商品自定义档案列表
   */
  @RequiresPermissions("product:customRecord:export")
  @Log(title = "商品自定义档案", businessType = BusinessType.EXPORT)
  @PostMapping("/export")
  @ResponseBody
  public AjaxResult export(ProCustomRecord proCustomRecord) {

    List<ProCustomRecordBiz>      list = proCustomRecordService.selectProCustomRecordList(proCustomRecord);
    ExcelUtil<ProCustomRecordBiz> util = new ExcelUtil<ProCustomRecordBiz>(ProCustomRecordBiz.class);
    return util.exportExcel(list, "customRecord");
  }

  /**
   * 新增商品自定义档案
   */
  @GetMapping(value = {"/add/{typeId}", "/add"})
  public String add(@PathVariable(value = "typeId", required = false) Long typeId,
                    ModelMap mmap) {

    if (StringUtils.isNotNull(typeId)) {
      mmap.put("proType", proTypeService.selectProTypeById(typeId));
    }
    return prefix + "/add";
  }


  /**
   * 新增保存商品自定义档案
   */
  @RequiresPermissions("product:customRecord:add")
  @Log(title = "商品自定义档案", businessType = BusinessType.INSERT)
  @PostMapping("/add")
  @ResponseBody
  public AjaxResult addSave(ProCustomRecord proCustomRecord) {

    return toAjax(proCustomRecordService.insertProCustomRecord(proCustomRecord));
  }

  /**
   * 修改商品自定义档案
   */
  @GetMapping("/edit/{customRecordId}")
  public String edit(@PathVariable("customRecordId") Long customRecordId,
                     ModelMap mmap) {

    ProCustomRecordBiz proCustomRecord = proCustomRecordService.selectProCustomRecordById(customRecordId);
    mmap.put("proCustomRecord", proCustomRecord);
    return prefix + "/edit";
  }

  /**
   * 修改保存商品自定义档案
   */
  @RequiresPermissions("product:customRecord:edit")
  @Log(title = "商品自定义档案", businessType = BusinessType.UPDATE)
  @PostMapping("/edit")
  @ResponseBody
  public AjaxResult editSave(ProCustomRecord proCustomRecord) {

    return toAjax(proCustomRecordService.updateProCustomRecord(proCustomRecord));
  }

  /**
   * 删除商品自定义档案
   */
  @RequiresPermissions("product:customRecord:remove")
  @Log(title = "商品自定义档案", businessType = BusinessType.DELETE)
  @PostMapping("/remove")
  @ResponseBody
  public AjaxResult remove(String ids) {

    return toAjax(proCustomRecordService.deleteProCustomRecordByIds(ids));
  }

  /**
   * 查询商品自定义档案列表
   */
  @RequiresPermissions("product:customRecord:list")
  @PostMapping("/find")
  @ResponseBody
  public List<ProCustomRecordBiz> find(ProCustomRecord proCustomRecord) {
    return proCustomRecordService.selectProCustomRecordList(proCustomRecord);
  }

}
